﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace instantget
{
    public partial class Targyfelvetel : Form
    {
        string hibak="";
        string user;
        int pass;

        public Targyfelvetel(string user, int pass)
        {
            InitializeComponent();
            this.user = user;
            this.pass = pass;
            nudindlicit.Maximum = (decimal)int.MaxValue;
            nudkivar.Maximum = (decimal)int.MaxValue;
        }

        private void btnFelvetel_Click(object sender, EventArgs e)
        {
            if (this.Validate() == true)
            {
                instantgetDataSet.licittargyRow ujtargy = LicitTargyFelvet();

                InduloLicitFelvet(ujtargy);

                KepekFelvet(ujtargy);

                MessageBox.Show("A tárgy felvétele sikeresen megtörtént!");
            }
            else
            {
                MessageBox.Show("A tárgy felvétele nem sikerült! \n"+hibak,"Tárgyfelvétel");
            }
        }

        private void KepekFelvet(instantgetDataSet.licittargyRow ujtargy)
        {
            foreach (PictureBox pb in panel1.Controls)
            {
                instantgetDataSet.kepRow kep = this.instantgetDataSet.kep.NewkepRow();
                kep.ta_id = ujtargy.ta_id;
                if (pb.ImageLocation.EndsWith(".gif"))
                {
                    kep.image = KepToByte(pb.Image, System.Drawing.Imaging.ImageFormat.Gif);
                }
                else if (pb.ImageLocation.EndsWith(".jpg") || pb.ImageLocation.EndsWith(".jpeg"))
                {
                    kep.image = KepToByte(pb.Image, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                else
                {
                    kep.image = KepToByte(pb.Image, System.Drawing.Imaging.ImageFormat.Png);
                }

                this.instantgetDataSet.kep.Rows.Add(kep);
                this.kepTableAdapter.Update(this.instantgetDataSet.kep);
            }
        }

        public byte[] KepToByte(Image kep, System.Drawing.Imaging.ImageFormat imageformat)
        {
            MemoryStream ms = new MemoryStream();
            kep.Save(ms, imageformat);
            return ms.ToArray();
        }

        private void InduloLicitFelvet(instantgetDataSet.licittargyRow ujtargy)
        {
            instantgetDataSet.licitRow induloLicit = this.instantgetDataSet.licit.NewlicitRow();
            induloLicit.ta_id = ujtargy.ta_id;
            induloLicit.pw = pass;
            induloLicit.nnev = user;
            induloLicit.idobelyeg = DateTime.Now;
            induloLicit.licit_ar = (int)nudindlicit.Value;
            induloLicit.tulaj = true;

            this.instantgetDataSet.licit.Rows.Add(induloLicit);
            this.licitTableAdapter.Update(this.instantgetDataSet.licit);
        }

        private instantget.instantgetDataSet.licittargyRow LicitTargyFelvet()
        {
            instantgetDataSet.licittargyRow ujtargy = this.instantgetDataSet.licittargy.NewlicittargyRow();
            ujtargy.ta_nev = txttargynev.Text;
            ujtargy.aukc_kezdet = DateTime.Today;
            ujtargy.auck_veg = dtpaukciovege.Value;
            if (rtbleiras.Text != "")
            {
                ujtargy.egyeb = rtbleiras.Text;
            }
            else
            {
                //TODO
                //ujtargy.egyeb = System.Data.SqlDbType.
            }
            ujtargy.alkat_id = (int)cbalkat.SelectedValue;
            if (checkBox1.Checked)
            {
                ujtargy.kivasarlas_ar = (int)nudkivar.Value;
            }
            ujtargy.elkelt = false;
            this.instantgetDataSet.licittargy.Rows.Add(ujtargy);
            this.licittargyTableAdapter.Update(this.instantgetDataSet.licittargy);
            return ujtargy;
        }

        private void Targyfelvetel_Load(object sender, EventArgs e)
        {
            AdapterFilling();
            dtpaukciovege.Value = DateTime.Today + new TimeSpan(7,0,0,0);
            toolTip1.SetToolTip(txttargynev, "3 és 75 karakter között legyen a hossza");
            toolTip1.SetToolTip(btnKep, "Maximum 6 képet vehet föl");
        }

        private void AdapterFilling()
        {
            this.kepTableAdapter.Fill(this.instantgetDataSet.kep);
            this.licitTableAdapter.Fill(this.instantgetDataSet.licit);
            this.alkategoriaTableAdapter.Fill(this.instantgetDataSet.alkategoria);
            this.fokategoriaTableAdapter.Fill(this.instantgetDataSet.fokategoria);
            this.licittargyTableAdapter.Fill(this.instantgetDataSet.licittargy);
        }

        private void cbfokat_SelectedIndexChanged(object sender, EventArgs e)
        {
           /* var kat = from f in instantgetDataSet.fokategoria
                      join a in instantgetDataSet.alkategoria on f.fokat_id equals a.fokat_id 
                      where f.fokat_id==Convert.ToInt32(cbfokat.SelectedValue)
                      select new {Alkatnev=a.alkat_nev};*/
            if (!cbfokat.Disposing)
            {
                this.alkategoriaTableAdapter.FillByFokategoria(this.instantgetDataSet.alkategoria, (int)cbfokat.SelectedValue);
            }
            cbalkat.Enabled = true;
            cbalkat.DisplayMember = "alkat_nev";
            cbalkat.ValueMember = "alkat_id";
            cbalkat.DataSource = alkategoriaBindingSource;

        }
        int i = 0;
        private void btnKep_Click(object sender, EventArgs e)
        { 
            OpenFileDialog kepes = new OpenFileDialog();
            kepes.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            kepes.Title = "Válaszd ki a képet!";
           
            
            if (kepes.ShowDialog() == DialogResult.OK)
            {
                PictureBox kep = new PictureBox();
                kep.ImageLocation = kepes.FileName;
                    kep.Size = new Size(128,128);
                    if (i<3)
                    {
                         kep.Location = new Point(0 + i * 130, 0);
                        panel1.Controls.Add(kep);
                        kep.SizeMode = PictureBoxSizeMode.Zoom;
                    }
                    else if (i < 6)
                    {
                        kep.Location = new Point(0 + (i-3) * 130, 130);
                        panel1.Controls.Add(kep);
                        kep.SizeMode = PictureBoxSizeMode.Zoom;
                    }
                    else
                    {
                        btnKep.Enabled = false;
                        btnKep.Text = "Több képet nem adhat hozzá!";
                    }
                    
                    
                i++;
            }
        }


        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox Check = (CheckBox)sender;
            if (Check.Checked)
            {
                nudkivar.Enabled = true;
            }
            else
            {
                nudkivar.Enabled = false;
            }
        }

        private void btnFelvetel_Validating(object sender, CancelEventArgs e)
        {
            Regex targyteszt = new Regex(@"^[-0-9a-zA-ZáéíóőúűÁÉÍÓŐÚŰ/.]{3,75}$");
            if (targyteszt.IsMatch(txttargynev.Text))
            {
                e.Cancel = false;
            }
            else
            {
                hibak = "Túl rövid vagy hosszú a megadott tárgynév! \n";
                e.Cancel = true;
            }
            if (dtpaukciovege.Value > DateTime.Today)
            {
                e.Cancel = false;
            }
            else
            {
                hibak += "Nem adhatsz meg múltbeli dátumot! \n";
                e.Cancel = true;
            }
            Regex indlicit = new Regex(@"^[1-9]{1}");
            if (indlicit.IsMatch(nudindlicit.Value.ToString()))
            {
                e.Cancel = false;
            }
            else
            {
                hibak += "Nem kezdődhet 0-val az induló licit! \n";
                e.Cancel = true;
            }

        }
    }
}
